<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>调车系统 API 测试</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .api-section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 8px;
            background: #fafafa;
        }
        .api-title {
            font-size: 18px;
            font-weight: bold;
            color: #2c3e50;
            margin-bottom: 15px;
        }
        .api-url {
            background: #34495e;
            color: white;
            padding: 10px;
            border-radius: 5px;
            font-family: monospace;
            margin-bottom: 15px;
        }
        .test-btn {
            background: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 5px;
            cursor: pointer;
            margin-right: 10px;
        }
        .test-btn:hover {
            background: #2980b9;
        }
        .result {
            margin-top: 15px;
            padding: 15px;
            background: #ecf0f1;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: monospace;
            max-height: 300px;
            overflow-y: auto;
        }
        .status {
            margin-top: 10px;
            padding: 10px;
            border-radius: 5px;
            text-align: center;
            font-weight: bold;
        }
        .status.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .status.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🚗 调车系统 API 测试页面</h1>
        
        <div class="api-section">
            <div class="api-title">1. 获取司机列表</div>
            <div class="api-url">GET /api/drivers</div>
            <button class="test-btn" onclick="testAPI('/api/drivers', 'GET', null, 'drivers-result')">测试接口</button>
            <div id="drivers-result" class="result" style="display:none;"></div>
            <div id="drivers-status" class="status" style="display:none;"></div>
        </div>

        <div class="api-section">
            <div class="api-title">2. 查询司机信息</div>
            <div class="api-url">GET /api/driver-info?name=张师傅</div>
            <button class="test-btn" onclick="testAPI('/api/driver-info?name=张师傅', 'GET', null, 'driver-info-result')">测试接口</button>
            <div id="driver-info-result" class="result" style="display:none;"></div>
            <div id="driver-info-status" class="status" style="display:none;"></div>
        </div>

        <div class="api-section">
            <div class="api-title">3. 获取调车人列表</div>
            <div class="api-url">GET /api/dispatchers</div>
            <button class="test-btn" onclick="testAPI('/api/dispatchers', 'GET', null, 'dispatchers-result')">测试接口</button>
            <div id="dispatchers-result" class="result" style="display:none;"></div>
            <div id="dispatchers-status" class="status" style="display:none;"></div>
        </div>

        <div class="api-section">
            <div class="api-title">4. 获取用车记录列表</div>
            <div class="api-url">GET /api/car-records</div>
            <button class="test-btn" onclick="testAPI('/api/car-records', 'GET', null, 'records-result')">测试接口</button>
            <div id="records-result" class="result" style="display:none;"></div>
            <div id="records-status" class="status" style="display:none;"></div>
        </div>

        <div class="api-section">
            <div class="api-title">5. 微信登录测试</div>
            <div class="api-url">POST /api/wx-login</div>
            <button class="test-btn" onclick="testWxLogin()">测试接口</button>
            <div id="wx-login-result" class="result" style="display:none;"></div>
            <div id="wx-login-status" class="status" style="display:none;"></div>
        </div>
    </div>

    <script>
        async function testAPI(url, method, data, resultId) {
            const resultDiv = document.getElementById(resultId);
            const statusDiv = document.getElementById(resultId.replace('-result', '-status'));
            
            try {
                resultDiv.style.display = 'block';
                statusDiv.style.display = 'block';
                resultDiv.textContent = '请求中...';
                statusDiv.textContent = '正在发送请求...';
                statusDiv.className = 'status';
                
                const options = {
                    method: method,
                    headers: {
                        'Content-Type': 'application/json',
                        'Accept': 'application/json'
                    }
                };
                
                if (data) {
                    options.body = JSON.stringify(data);
                }
                
                const response = await fetch(url, options);
                const result = await response.json();
                
                resultDiv.textContent = JSON.stringify(result, null, 2);
                
                if (response.ok) {
                    statusDiv.textContent = `请求成功 (${response.status})`;
                    statusDiv.className = 'status success';
                } else {
                    statusDiv.textContent = `请求失败 (${response.status})`;
                    statusDiv.className = 'status error';
                }
            } catch (error) {
                resultDiv.textContent = '错误: ' + error.message;
                statusDiv.textContent = '网络错误或服务器无响应';
                statusDiv.className = 'status error';
            }
        }
        
        function testWxLogin() {
            const data = {
                code: 'test_code_123',
                userInfo: {
                    nickName: '测试用户',
                    avatarUrl: 'https://example.com/avatar.jpg'
                }
            };
            testAPI('/api/wx-login', 'POST', data, 'wx-login-result');
        }
    </script>
</body>
</html>